package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Course;
import com.qk.management.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author diva
 * @date 2025/10/8 19:58
 * @ProjectName
 */

@Slf4j
@RestController
@RequestMapping("/courses")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @GetMapping
    public Result page(@RequestParam(name = "subject", required = false) Integer subject,
                       @RequestParam(name = "name", required = false) String name,
                       @RequestParam(name = "price", required = false) Integer price,
                       @RequestParam(name = "target", required = false) Integer target,
                       @RequestParam(name = "description", required = false) String description,
                       @RequestParam(name = "page") Integer page,
                       @RequestParam(name = "pageSize") Integer pageSize) {
        PageResult<Course> pageResult = courseService.page(subject, name, price, target, description, page, pageSize);
        log.info("查询课程列表，参数：课程名称={}，课程价格={}，课程目标={}，课程描述={}，当前页码={}，页码大小={}",name,price,target,description,page,pageSize);
        return Result.success(pageResult);
    }

    @PostMapping
    public Result addDept(@RequestBody Course course) {
        log.info("新增部门，参数：{}",course);
        courseService.addCourse(course);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getCourseById(@PathVariable("id") Integer id) {
        log.info("查询课程id：{}",id);
        Course course = courseService.getCourseById(id);
        return Result.success(course);
    }
    @PutMapping
    public Result update(@RequestBody Course course) {
        log.info("修改课程：{}",course);
        courseService.update(course);
        return Result.success();
    }
    @DeleteMapping("/{id}")
    public Result deleteCourse(@PathVariable Integer id) {
        log.info("删除课程id：{}",id);
        courseService.deleteCourse(id);
        return Result.success();
    }

    @GetMapping("/list")
    public Result selectAll() {
        log.info("查询课程列表");
        List<Course> courseList = courseService.selectAll();
        return Result.success(courseList);
    }
    @GetMapping("/subject/{subject}")
    public Result getCourseBySubject(@PathVariable Integer subject) {
        log.info("查询课程主题：{}",subject);
        List<Course> courseList = courseService.getCourseBySubject(subject);
        return Result.success(courseList);
    }
}
